package com.exam.sacrifice.ui;

import android.content.Intent;
import android.net.Uri;
import android.view.View;

import com.exam.commonbiz.base.BaseFragment;
import com.exam.commonbiz.base.BaseViewModel;
import com.exam.commonbiz.util.CommonCallBack;
import com.exam.commonbiz.util.OnFastClickListener;
import com.exam.commonbiz.util.ToastUtil;
import com.exam.sacrifice.R;
import com.exam.sacrifice.databinding.FragmentHomeBinding;
import com.exam.sacrifice.event.PlayMusicEvent;
import com.exam.sacrifice.ui.dialog.JiSiActivityDialog;
import com.exam.sacrifice.util.RotationAnimation;

import org.greenrobot.eventbus.EventBus;

public class HomeFragment extends BaseFragment<BaseViewModel, FragmentHomeBinding> {

    private boolean isRotating = true;

    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public void initData() {

        initVideo();
        binding.ivJisi.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                ToastUtil.getInstance().showToast("功能开发中");
            }
        });

        binding.ivCitang.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                startActivity(new Intent(context, JiSiActivity.class));
//                startActivity(new Intent(context, GongPinActivity.class));
                getActivity().overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            }
        });

        binding.ivLiuli.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                startActivity(new Intent(context, LiuLiShiJieActivity.class));
                getActivity().overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            }
        });

        binding.ivMusic.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                if (!isRotating) {
                    RotationAnimation.rotateImage(binding.ivMusic);
                    isRotating = true;
                    EventBus.getDefault().post(new PlayMusicEvent(PlayMusicEvent.MUSIC_START));
                } else {
                    RotationAnimation.stopRotation(binding.ivMusic);
                    isRotating = false;
                    EventBus.getDefault().post(new PlayMusicEvent(PlayMusicEvent.MUSIC_STOP));
                }
            }
        });

        new JiSiActivityDialog().show(context, new CommonCallBack() {
            @Override
            public void callback(int postion, Object object) {
                String url = "http://fete.wqsoftware.cn/#/pages/activitDetail/activitDetail";
                X5WebViewActivity.start(context, "活动详情", url);
            }
        });

        RotationAnimation.rotateImage(binding.ivMusic);
    }

    public void playAnim() {
        RotationAnimation.rotateImage(binding.ivMusic);
        isRotating = true;
    }

    @Override
    protected void loadData() {

    }

    private void initVideo() {

        // 设置视频源 (res/raw/sample.mp4)
        Uri videoUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.home_bg);
        binding.videoView.setVideoURI(videoUri);

        // 设置监听器
        binding.videoView.setOnPreparedListener(mp -> {
            binding.videoView.start();
        });

        binding.videoView.setOnCompletionListener(mp -> {
            binding.videoView.start();
        });

        binding.videoView.setOnErrorListener((mp, what, extra) -> {
            ToastUtil.getInstance().showToast("播放错误");
            return true;
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        binding.videoView.stopPlayback();
    }
}
